import {
  getToken,
  setToken,
  removeToken,
  getUserInfo,
  setUserInfo,
  removeUserInfo
} from '@/utils/auth'
import router, { resetRouter } from '@/router'
import Vue from 'vue'

const getDefaultState = () => {
  return {
    token: getToken(),
    userInfo: getUserInfo(),
    msgList: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USER_INFO: (state, value) => {
    state.userInfo = value
  },
  SET_NEW_MSG: (state, msg) => {
    state.msgList = msg
  }
}

const actions = {
  // 获取新的消息
  getMsg ({ commit }) {
    Vue.prototype
      .$POST('one-travel-api/complaint/complaintSuggestionCount', {})
      .then(res => {
        if (res.errorCode === '00000') {
          const list = [
            {
              name: '待处理反馈',
              path: '/applets/contact-us/feedback?status=1',
              num: res.data.suggestion
            },
            {
              name: '待处理投诉',
              path: '/applets/contact-us/complaint?status=1',
              num: res.data.complaint
            },
            {
              name: '待处理合作',
              path: '/applets/contact-us/cooperation?status=0',
              num: res.data.cooperation
            }
          ]
          commit('SET_NEW_MSG', list)
        }
      })
  },
  login ({ commit }, from) {
    return new Promise((resolve, reject) => {
      Vue.prototype
        .$POST('sot-admin-api/sys/auth/accountLogin', from)
        .then(res => {
          if (res.data) {
            commit('SET_TOKEN', res.data.token)
            setToken(res.data.token)

            commit('SET_USER_INFO', res.data.userInfo)
            setUserInfo(res.data.userInfo)
          }
          resolve(res)
        })
        .catch(error => {
          console.error(error)
          reject(error)
        })
    })
  },
  phoneLogin ({ commit }, from) {
    return new Promise((resolve, reject) => {
      Vue.prototype
        .$POST('sot-admin-api/sys/auth/phoneLogin', from)
        .then(res => {
          if (res.data) {
            commit('SET_TOKEN', res.data.token)
            setToken(res.data.token)

            commit('SET_USER_INFO', res.data.userInfo)
            setUserInfo(res.data.userInfo)
          }
          resolve(res)
        })
        .catch(error => {
          console.error(error)
          reject(error)
        })
    })
  },

  logout ({ commit, state }, silent = false) {
    removeToken() // must remove token first
    removeUserInfo()
    resetRouter()
    commit('RESET_STATE')

    // 仅顶级系统支持无感重新登录
    if (!silent) {
      router.replace('/login').then(() => {
        window.location.reload()
      })
    }
  },

  resetToken ({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
